﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace SHKF.B2C.Erp.BLL.SugarIOC
{
    public class DbScoped
    {
        /// <summary>
        /// 服务实例名
        /// </summary>
        /// <param name="serverName"></param>
        /// <returns></returns>
        public static SqlSugarClient CreateSugar(string serverName)
        {
            List<ConnectionConfig> configs = SugarServiceExtensions.configs[serverName]; 
            SqlSugarClient result = new SqlSugarClient(configs);

            return result; 
        }

        private static SqlSugarClient GetSqlSugarClient(string serverName)
        {
            var sugar = IocCallContext<SqlSugarClient>.GetData(serverName);
            if (sugar == null)
            {
                List<ConnectionConfig> configs = new List<ConnectionConfig>();
                SqlSugarClient result = new SqlSugarClient(configs);

                IocCallContext<SqlSugarClient>.SetData(serverName, result);
                return result;
            }
            else
            {
                return sugar;
            }
        }
    }
}
